<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/21/0021
 * Time: 16:59
 */

namespace app\sports_admin\service;


use app\exception\PlanTypeException;
use think\Exception;
use think\Request;
use app\sports_admin\model\PlanType as PlanTypeModel;
use think\Db;
class PlayType
{
    /*
     * 教案分类首页
     */
    public static function index()
    {
        //按级别和排序字段升序查询所有信息
        $data = Db::table('plan_type')->order('level')->order('sort')->select();
        return self::arrToMenu($data,0);
    }
    /*
     * 递归分类
     * @param $data 要递归的数据
     * @param $pid 父类 id
     */
    public static function arrToMenu($data,$pid){
        static $menu = array();
        foreach($data as $k => $v){
            if($v['parent_id'] == $pid){
                $menu[] = $v;
                $v['parent_id'] = self::arrToMenu($data,$v['id']);
            }
        }
        return $menu;
    }
    /**
     * 添加父类
     * @param title 类别名称
     * @param path 路径
     * @param parent_id 父类名称
    */
    public static function parentInsert()
    {
        $param = Request::instance()->param();
//        if(!self::findTitle($param['title'])){
//            throw new Exception('服务器错误');
//        }
        $bool = PlanTypeModel::create([
            'title'=>$param['title'],
            'path'=>$param['path'],
            'level'=>$param['level'],
            'sort'=>$param['sort'],
            'parent_id'=>$param['parent_id']
        ]);
        if(!$bool){
            throw new PlanTypeException([
                'msg'=>'类别添加失败',
                'errorCode'=>'400'
            ]);
        }
        return json(['msg'=>'添加成功','code'=>200]);
    }
    /**
     * 添加子类
     * @param title 类别名称
     * @param path 路径
     * @param parent_id 父类id
     */
    public static function insert()
    {
        $param = Request::instance()->param();
//        if(!self::findTitle($param['title'])){
//            throw new Exception('服务器错误');
//        }
        $bool = PlanTypeModel::create([
            'title'=>trim($param['title']),
            'path'=>$param['path'],
            'level'=>$param['level'],
            'sort'=>$param['sort'],
            'parent_id'=>$param['parent_id']
        ]);
        if(!$bool){
            throw new PlanTypeException([
                'msg'=>'类别添加失败',
                'errorCode'=>'400'
            ]);
        }
        return json(['msg'=>'添加成功','code'=>200]);
    }
    /**
     * 添加子类
     * @param title 类别名称
     * @param path 路径
     * @param parent_id 父类id
     */
    public static function update()
    {
        $param = Request::instance()->param();
        if($param['title'] != $param['primaryTitle']){
            if(!self::findTitle($param['title'])){
                throw new Exception('服务器错误');
            }
        }
        //$bool = PlanTypeModel::where('id',$param['id'])->update(['title'=>$param['title']]);
        $bool = (new PlanTypeModel())->save(
            ['title'=>$param['title'],'level'=>$param['level'],'sort'=>$param['sort']],
            ['id'=>$param['id']]
        );
        if(!$bool){
            throw new PlanTypeException([
                'msg'=>'类别修改失败',
                'errorCode'=>'400'
            ]);
        }
        return json(['msg'=>'修改成功','code'=>200]);
    }
    /*
     * 修改类别父类
     */
    public static function updateTypeParent()
    {
        $params = Request::instance()->param();
        //查询父类信息                            parent_id 父类id
        $parentInfo = PlanTypeModel::where('id',$params['parent_id'])->find();
        $bool = (new PlanTypeModel())->save(
            ['path'=>$parentInfo->path.$params['parent_id'].',','parent_id'=>$params['parent_id']],
            ['id'=>$params['id']]
        );
        if(!$bool){
            throw new PlanTypeException([
                'msg'=>'修改失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>200,'msg'=>'修改成功']);
    }
    /*
     * 修改类别排序
     */
    public static function sort()
    {
        $params = Request::instance()->param();
        $bool = (new PlanTypeModel())->save(
            ['sort'=>$params['sorts']],//排序的级别
            ['id'=>$params['id']] //类别id号
        );
        if(!$bool){
            throw new PlanTypeException([
                'msg'=>'修改失败',
                'errorCode'=>400
            ]);
        }
        return json(['code'=>200,'msg'=>'修改成功']);
    }
    /**
     * 查询类别是否存在
     * @param $title 类别名称
    */
    private static function findTitle($title)
    {
        $bool = PlanTypeModel::where('title',$title)->find();
        if($bool){
            throw new PlanTypeException([
                'msg'=>'类别已存在',
                'errorCode'=>400
            ]);
        }
        return true;
    }
}